//
//  LSFirstCell.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/8/22.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSFirstCell.h"

@interface LSFirstCell ()<CAAnimationDelegate>

@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIImageView *lineImageView;
@property (nonatomic,strong) UIButton *btn;

@end

@implementation LSFirstCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor clearColor];
        UIButton *btn = [[UIButton alloc] init];
        self.btn = btn;
        CGFloat btnWidth = 101*ScalesH;
        CGFloat btnHeight = 101*ScalesH;//(kHeight-kNavgationBar-100*ScalesH)/3-20-30*ScalesH-50;
        btn.layer.cornerRadius = btnWidth/2;
        btn.layer.masksToBounds = YES;
      //  btn.backgroundColor = [UIColor redColor];
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(CGSizeMake(btnWidth, btnHeight));
            make.top.equalTo(self);
            make.centerX.equalTo(self.mas_centerX);
        }];
        [btn addTarget:self action:@selector(pushAction:) forControlEvents:UIControlEventTouchUpInside];
        
        self.titleLabel = [[UILabel alloc] init];
        self.titleLabel.textColor = [UIColor whiteColor];
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        self.titleLabel.font = [UIFont systemFontOfSize:16.0f*ScalesW];
        [self addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(btn.mas_bottom).offset(5*ScalesW);
            make.left.right.equalTo(self);
            make.height.mas_equalTo(20);
        }];
        
        self.lineImageView = [[UIImageView alloc] init];
        self.lineImageView.image = [UIImage imageNamed:@"LS-箭头"];
        [self addSubview:self.lineImageView];
        [self.lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.titleLabel.mas_bottom).offset(2*ScalesW);
            make.centerX.equalTo(self.mas_centerX);
            make.bottom.equalTo(self).offset(-10);
            make.width.mas_equalTo(7);
        }];
    }
    
    return self;
}

- (void)setDict:(NSDictionary *)dict {
    _dict = dict;
    NSNumber *indexPathRow = dict[@"IndexPathRow"];
    
    NSString *typeID = dict[@"TypeID"];
    NSArray *names1 = [NSArray array];
    NSArray *names2 = [NSArray array];
    if ([typeID isEqualToString:@"SE"]) {
        names1 = @[@"LS-慢速听力训练",@"LS-中速听力训练",@"LS-快速听力训练"];
        names2 = @[@"LS-慢速听力训练2",@"LS-中速听力训练2",@"LS-快速听力训练2"];
    } else if ([typeID isEqualToString:@"Sp"]||[typeID isEqualToString:@"Spn"]) {
        names1 = @[@"LS-慢速朗读训练",@"LS-中速朗读训练",@"LS-快速朗读训练"];
        names2 = @[@"LS-慢速朗读训练2",@"LS-中速朗读训练2",@"LS-快速朗读训练2"];
    }
    
    [self.btn setBackgroundImage:[UIImage imageNamed:names1[indexPathRow.integerValue]] forState:UIControlStateNormal];
     self.titleLabel.text = [names1[indexPathRow.integerValue] stringByReplacingOccurrencesOfString:@"LS-" withString:@""];

     if (indexPathRow.integerValue==2) {
        self.lineImageView.alpha = 0;
    }
  
    NSNumber *stage = dict[@"Stage"];
    if(indexPathRow.integerValue!=(stage.integerValue-1)) {
        
        _btn.userInteractionEnabled = NO;
        [self.btn setBackgroundImage:[UIImage imageNamed:names2[indexPathRow.integerValue]] forState:UIControlStateNormal];
        if(indexPathRow.integerValue<stage.integerValue-1) {
            [self.btn setBackgroundImage:[UIImage imageNamed:@"LS-已通关"] forState:UIControlStateNormal];
        }
        
    } else {
        _btn.userInteractionEnabled = YES;
    }
    
}

- (void)scaleAnimationWithView:(UIView*)view {
    CABasicAnimation*pulse = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    pulse.delegate = self;
    pulse.timingFunction= [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    pulse.duration = 0.2;
    pulse.repeatCount= 1;
    pulse.autoreverses= NO;
    pulse.fromValue= [NSNumber numberWithFloat:1.2];
    pulse.toValue= [NSNumber numberWithFloat:0.8];
    [view.layer addAnimation:pulse forKey:@"BtnScale"];
}

- (void)pushAction:(UIButton*)sender {
    
    [sender.layer removeAnimationForKey:@"BtnScale"];
    [self scaleAnimationWithView:sender];
    
   // self.pushBlock();
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
    if (flag) {
        self.pushBlock();
    }
}

@end
